package com.voute.App;

import android.app.Activity;
import android.app.Application;
import android.content.Intent;

import com.umeng.analytics.MobclickAgent;
import com.umeng.socialize.Config;
import com.umeng.socialize.PlatformConfig;
import com.voute.Service.LocationService;
import com.zxinsight.MWConfiguration;
import com.zxinsight.MagicWindowSDK;
import com.zxinsight.Session;

import java.util.ArrayList;
import java.util.List;

import cn.jpush.android.api.JPushInterface;

/**
 * Application类
 * Created by liukanshan on 2016/12/10.
 */

public class App extends Application {

    public static App instance;
    /**
     * 打开的activity
     **/
    private List<Activity> activities = new ArrayList<Activity>();

    public static App GetInstance() {
        synchronized (App.class) {
            if (instance == null) {
                instance = new App();
            }
        }
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        Config.DEBUG = true;
        PlatformConfig.setWeixin("wxe9cc7c18f316ca86", "bb566db2c41afd918ea2850643d482ed");
        PlatformConfig.setSinaWeibo("2322102796", "16eb737c535dc66c865b7cc4663cba78", "https://www.anyknew.com");
//        PlatformConfig.setSinaWeibo("2322102796","16eb737c535dc66c865b7cc4663cba78");
        PlatformConfig.setQQZone("1106014272", "b8BivUcHJw1egUps");
        //定位service启动
        Intent intent = new Intent(this, LocationService.class);//加快启动速度，在service中定位
        startService(intent);
        JPushInterface.setDebugMode(true);    // 设置开启日志,发布时请关闭日志
        JPushInterface.init(this);            // 初始化 JPush
        MobclickAgent.setDebugMode(true);//
        initMw();
        Session.setAutoSession(this);
//         MobclickAgent.startWithConfigure(
//         new MobclickAgent.UMAnalyticsConfig(this, "58bd6f88310c93235b0010a2", "Umeng",
//         MobclickAgent.EScenarioType.E_UM_NORMAL));
    }

    private void initMw(){
        MWConfiguration config = new MWConfiguration(this);
        config.setChannel("Normal")
                .setDebugModel(true)
                .setPageTrackWithFragment(true)
                .setWebViewBroadcastOpen(true)
                .setCustomWebViewTitleBarOn()
                .setSharePlatform(MWConfiguration.ORIGINAL)
                .setMLinkOpen();

        MagicWindowSDK.initSDK(config);
    }

    /**
     * 新建了一个activity
     *
     * @param activity
     */
    public void addActivity(Activity activity) {
        activities.add(activity);
    }

    /**
     * 结束指定的Activity
     *
     * @param activity
     */
    public void finishActivity(Activity activity) {
        if (activity != null) {
            this.activities.remove(activity);
            activity.finish();
            activity = null;
        }
    }

    /**
     * 应用退出，结束所有的activity
     */
    public void exit() {
        for (Activity activity : activities) {
            if (activity != null) {
                activity.finish();
            }
        }
        System.exit(0);
    }

    /**
     * 关闭Activity列表中的所有Activity
     */
    public void finishActivity() {
        for (Activity activity : activities) {
            if (null != activity) {
                activity.finish();
            }
        }
        //杀死该应用进程
        android.os.Process.killProcess(android.os.Process.myPid());
    }

    /**
     * 结束指定类名的Activity
     */
    public void finishClassActivity(Class<?> cls) {
        for (Activity activity : activities) {
            if (activity.getClass().getName().equals(cls.getName())) {
                finishActivity(activity);
                return;
            }
        }
    }

    public void finishOtherActivity(Class<?> cls){
        for (Activity activity : activities) {
            if (!activity.getClass().getName().equals(cls.getName())) {
                finishActivity(activity);
                return;
            }
        }
    }

    public int getActivityCount() {
        if (activities == null) {
            return 0;
        } else {
            return activities.size();
        }
    }

    public List<Activity> getActivities(){
        return activities;
    }
}
